#define _CRT_SECURE_NO_WARNINGS 1
#include"Queue.h"

void Queueinit(Que* pq)
{
	assert(pq);
	pq->qhead = NULL;
	pq->qtail = NULL;
	pq->size = 0;
}

void Queuepush(Que* pq, Qdata x)
{
	assert(pq);
	QN* newnode = (QN*)malloc(sizeof(QN));
	if (newnode == NULL)
	{
		perror("malloc");

		return;
	}
	newnode->val = x;
	newnode->next = NULL;
	if (pq->qtail == NULL)
	{
		pq->qtail =pq->qhead= newnode;
	}
	else
	{
		pq->qtail->next = newnode;
		pq->qtail = newnode;
	}
	pq->size++;
}

int QueueEmpty(Que* pq)
{
	assert(pq);
	return pq->size == 0;
}

void Queuepop(Que* pq)
{
	assert(pq);
	assert(pq->size != 0);

	if (pq->qhead->next == NULL)
	{
		free(pq->qhead);
		pq->qhead = pq->qtail = NULL;
	}
	else
	{
		QN* next = pq->qhead->next;
		free(pq->qhead);
		pq->qhead = next;
	}
	pq->size--;
}

void Queuedestroy(Que* pq)
{
	assert(pq);

	QN* cur = pq->qhead;
	while (cur)
	{
		QN* next = cur->next;
		free(cur);
		cur = next;
	}
	pq->qhead = pq->qtail = NULL;
	pq->size = 0;
}

int Queuesize(Que* pq)
{
	assert(pq);

	return pq->size;
}

Qdata QueueFront(Que* pq)
{
	assert(pq);
	return pq->qhead->val;
}

Qdata QueueBack(Que* pq)
{
	assert(pq);
	return pq->qtail->val;
}
